package com.lks.users.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import com.lks.common.util.PageInfoUtil;
import com.lks.users.dao.IAAdminsdao;
import com.lks.users.pojo.AAdmins;
import com.lks.users.service.IUsersService;

@Controller("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsdao adminsdao;

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsdao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.adminsdao.insert(admins);
		if (insert > 0)
		{
			resultJSON.put("info", "管理员信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.adminsdao.update(admins);
		if (update > 0)
		{
			resultJSON.put("info", "管理员信息更新成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.adminsdao.insert(admins);
		if (update > 0)
		{
			resultJSON.put("info", "管理员信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AAdmins> adminsList = new ArrayList<AAdmins>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			//先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			//查询总记录数
			adminsList = this.adminsdao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			
			/*获取分页数据*/
			condMap.put("pageCount", "false");
			/*分页信息*/
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			//查询的数据
			adminsList = this.adminsdao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			adminsList = this.adminsdao.findCondList(condMap);
		}
		return adminsList;
	}

}
